package com.hotel.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.auto.JSONAuthentication;
import com.hotel.entity.HoHouse;
import com.hotel.entity.HoPass;
import com.hotel.mapper.HoPassMapper;
import com.hotel.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/ho-pass")
public class HoPassController extends JSONAuthentication {
    @Autowired
    HoPassMapper hoPassMapper;

    @ApiOperation(value = "添加旅客")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response,@RequestBody  HoPass hoPass) throws Exception{
        if(hoPassMapper.insert(hoPass)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }

    @ApiOperation(value = "删除旅客")
    @PostMapping("/del")
    public void delOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=hoPassMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有旅客")
    @PostMapping("/findAllPass")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<HoPass> shUserPage=new Page<>(current,size);
        Page<HoPass> shUserPages=hoPassMapper.selectPage(shUserPage,null);
        List<HoPass> list = shUserPages.getRecords();
        int total=hoPassMapper.selectCount(null);
        if(list.size()>0){
            Result ok = Result.ok().data("userList",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

